// pages/finishCoupon/finishCoupon.js
const http = require('../../network/http')
const api = require('../../network/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeId: '',
    detailId: '',
    memberId: '',
    couponList: [],
    couponId: [],
    pageNumber: 1,
    pages: 0,
  },
    /**
   * 获取优惠券列表
   */
  getGiveList() {
    http.request({
      showLoading: true,
      url: api.getGiveList,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        pageNumber: this.data.pageNumber,
        pageSize: 20,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("优惠券表数据返回:");
        console.log(res.data.result);
        var newList = this.data.couponList;
        if(res.data.result.records != null && res.data.result.records.length > 0){
          for(var i=0; i < res.data.result.records.length; i++){
            var coupon = {
              isSel: 0,
              id: "",
              briefDesc: "",
              couponDesc: "",
              couponType: "",
              durationTime: "",
              enabled: "",
              expireDays: "",
              expireTime: "",
              grouponType: "",
              merchantId: "",
              price: "",
              quantity: "",
              remainderQuantity: "",
              saleable: "",
              skuId: "",
              soldEndTime: "",
              soldQuantity: "",
              soldStartTime: "",
              storeId: "",
              title: "",
              type: "",
              version: "",
            }
            coupon.id = res.data.result.records[i].id;
            coupon.briefDesc = res.data.result.records[i].briefDesc;
            coupon.couponDesc = res.data.result.records[i].couponDesc;
            coupon.couponType = res.data.result.records[i].couponType;
            coupon.durationTime = res.data.result.records[i].durationTime;
            coupon.enabled = res.data.result.records[i].enabled;
            coupon.expireDays = res.data.result.records[i].expireDays;
            coupon.expireTime = res.data.result.records[i].expireTime;
            coupon.grouponType = res.data.result.records[i].grouponType;
            coupon.merchantId = res.data.result.records[i].merchantId;
            coupon.price = res.data.result.records[i].price;
            coupon.quantity = res.data.result.records[i].quantity;
            coupon.remainderQuantity = res.data.result.records[i].remainderQuantity;
            coupon.saleable = res.data.result.records[i].saleable;
            coupon.skuId = res.data.result.records[i].skuId;
            coupon.soldEndTime = res.data.result.records[i].soldEndTime;
            coupon.soldQuantity = res.data.result.records[i].soldQuantity;
            coupon.soldStartTime = res.data.result.records[i].soldStartTime;
            coupon.storeId = res.data.result.records[i].storeId;
            coupon.title = res.data.result.records[i].title;
            coupon.type = res.data.result.records[i].type;
            coupon.version = res.data.result.records[i].version;

            newList.push(coupon);
          }
        }
        this.setData({
          'couponList': newList,
          pages: res.data.result.pages,
          // totalPrice: res.data.result[0].monthTotalPrice,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });

  }, 
  clickItem: function(e){
    var $data = e.currentTarget.dataset;
    var index = e.currentTarget.dataset.index;
    console.log(JSON.stringify($data));
    var newCoupon = this.data.couponList[index]
    if(newCoupon.isSel == 0){
      newCoupon.isSel = 1;
    }else{
      newCoupon.isSel = 0;
    }
    this.data.couponList[index] = newCoupon;
    this.setData({
      couponList: this.data.couponList,
    })
  }, 
  confirmChoose: function(e){
    var chooseIds = [];
    for(var i=0; i < this.data.couponList.length; i++){
      if(this.data.couponList[i].isSel == 1){
        chooseIds.push(this.data.couponList[i].id);
      }
    }
    if(chooseIds.length == 0){
      setTimeout(function () {
        wx.showToast({
          title: '请选择优惠券',
          icon: "none",
          duration: 2000
        });
      }, 500);
      return;
    }
    http.request({
      showLoading: true,
      shake: true,
      url: api.doFinish,
      method: "POST",
      data: {
        memberId: this.data.memberId,
        tradeId: this.data.detailId,
        isGive: true,
        couponIdList: chooseIds,
      },
    }).then(res => {
      if (res.data.code == 200) {
        if(res.data.result){
          setTimeout(function () {
            wx.showToast({
              title: '订单完成',
              icon: "none",
              duration: 2000
            });
          }, 500);
          wx.navigateBack({
            delta: 2,
          })
        }else{
          setTimeout(function () {
            wx.showToast({
              title: '操作失败，请稍后再试',
              icon: "none",
              duration: 2000
            });
          }, 500);
        }
      }else{
        setTimeout(function () {
          wx.showToast({
            title: '操作失败，请稍后再试',
            icon: "none",
            duration: 2000
          });
        }, 500);
      }
    }).catch(err => {
      console.log(err);
      setTimeout(function () {
        wx.showToast({
          title: err,
          icon: "none",
          duration: 2000
        });
      }, 1000);
    });
  },
  closeview: function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    that.setData({
      storeId: options.storeId,
      detailId: options.detailId,
      memberId: options.memberId,
      pageNumber: 1,
      pages: 0,
      'couponList': [],
    })
    this.getGiveList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})